package com.projet.whattowatch.objet;

import java.util.ArrayList;

import com.projet.whattowatch.activity.ConnectedActivity;

public class LibFilm {

	private ArrayList<Film> listFilm;

	public LibFilm() {
		this.listFilm = new ArrayList<Film>();
	}

	public ArrayList<Film> getListFilm() {
		return this.listFilm;
	}

	public Film getFilm(String id) {
		String[] idTable = id.split("-");	
		String monNom = idTable[0];
		String monRealisateur = idTable[1];

		for (int i = 0; i < listFilm.size(); i++) {
			if (listFilm.get(i).getNom().compareTo(monNom) == 0
					&& listFilm.get(i).getRealisateur(0).getNom()
							.compareTo(monRealisateur) == 0)
			{
				return listFilm.get(i);
			}
		}
		return null;
	}

	public Film getFilm(int index) {
		return listFilm.get(index);
	}

	public boolean remove(Film monFilm) {
		return listFilm.remove(monFilm);
	}

	public void addFilm(Film monFilm) {
		int debut = 0;
		int fin = this.getListFilm().size() - 1;
		recAddFilm(debut, fin, monFilm);
	}

	public void recAddFilm(int debut, int fin, Film monFilm) {
		int milieu = (fin + debut) / 2;
		if (fin < 0) {
			listFilm.add(milieu, monFilm);
		} else if (fin == 0) {
			if (monFilm.compareTo(listFilm.get(milieu)) > 0) {
				listFilm.add(milieu, monFilm);
			} else {
				listFilm.add(monFilm);
			}
		} else if (monFilm.compareTo(listFilm.get(milieu)) > 0) {
			if (debut == milieu) {
				listFilm.add(milieu, monFilm);
			} else
				recAddFilm(debut, milieu, monFilm);
		} else {
			if (milieu == debut) {
				if (monFilm.compareTo(listFilm.get(fin)) > 0) {
					listFilm.add(fin, monFilm);
				} else {
					listFilm.add(monFilm);
				}
			} else
				recAddFilm(milieu, fin, monFilm);
		}
	}

	public ArrayList<String> search(String keyword, String critere) {
		ArrayList<String> resultat = new ArrayList<String>();
		if (critere.equals("Name"))
		{
			String keywordLowerCase = keyword.toLowerCase();
			resultat = new ArrayList<String>();
			for (int i = 0; i < listFilm.size(); i++) {
				String listFilmLowerCase = listFilm.get(i).getNom().toLowerCase();
				if (listFilmLowerCase.contains(keywordLowerCase)) {
					resultat.add(listFilm.get(i).getNom());
				}
			}
			return resultat;
		}
		if (critere.equals("Director"))
		{
			String keywordLowerCase = keyword.toLowerCase();
			resultat = new ArrayList<String>();
			for (int i = 0; i < listFilm.size(); i++) {
				ArrayList <Humain> listRealisateur = listFilm.get(i).getListRealisateur();
				for (int j = 0; j < listRealisateur.size() ; j ++)
				{
					 String nameRealisateurToLowerCase = listRealisateur.get(j).getNom().toLowerCase() + listRealisateur.get(j).getPrenom().toLowerCase();
					if (nameRealisateurToLowerCase.contains(keywordLowerCase)) {
						boolean alreadyExist = false;
						for (int k = 0; k < resultat.size(); k++) 
						{
							
							if (listFilm.get(i).getNom().equals(resultat.get(k)))								
							{
								alreadyExist = true;
							}
						}
						if(!alreadyExist) resultat.add(listFilm.get(i).getNom());
					}
				}
			}
			return resultat;
		}
		if (critere.equals("Years"))
		{
			resultat = new ArrayList<String>();
			if (!isInteger(keyword))
			{
				if (keyword.equals(""))
				{
					ArrayList<String> newList = new ArrayList<String>();
					for (int i = 0; i < listFilm.size(); i++) 
					{
						newList.add(listFilm.get(i).getNom());
					}
					return newList;
				}
				return resultat;
			}
				for (int i = 0; i < listFilm.size(); i++) {
					int years = listFilm.get(i).getDate();
					if (years == Integer.parseInt(keyword)) {
						resultat.add(listFilm.get(i).getNom());
					}
				}
			return resultat;
			
			
		}
		if (critere.equals("Genre"))
		{
			String keywordLowerCase = keyword.toLowerCase();
			for (int i = 0; i < listFilm.size(); i++) {
				ArrayList <String> listGenre = listFilm.get(i).getListGenre();
				for (int j = 0; j < listGenre.size() ; j ++)
				{
					 String nameGenreToLowerCase = listGenre.get(j).toLowerCase();
					if (nameGenreToLowerCase.contains(keywordLowerCase)) {
						boolean alreadyExist = false;
						for (int k = 0; k < resultat.size(); k++) 
						{
							
							if (listFilm.get(i).getNom().equals(resultat.get(k)))								
							{
								alreadyExist = true;
							}
						}
						if(!alreadyExist) resultat.add(listFilm.get(i).getNom());
					}
				}
			}
			return resultat;
		}
		
		return resultat;
	}

	
	public static boolean isInteger(String s) {
	    try { 
	        Integer.parseInt(s); 
	    } catch(NumberFormatException e) { 
	        return false; 
	    }
	    // only got here if we didn't return false
	    return true;
	}


	public ArrayList<String> advancedSearch(String titre, String directeur,
			String genre, String years) {
		// String[] array ={"Titre","Directeur","Genre","Years"};
		ArrayList<String> stringFilm = new ArrayList<String>();
		LibFilm newLibFilm = new LibFilm();
		stringFilm = this.search(titre, "Name");
		ArrayList<Film> maLibFilm = new ArrayList<Film>();
		for (int j = 0; j < stringFilm.size(); j++) {
			for (int j2 = 0; j2 < listFilm.size(); j2++) {

				String filmLowerCase = stringFilm.get(j);
				if (listFilm.get(j2).getNom().equals(filmLowerCase)) {

					boolean alreadyExist = false;
					for (int k = 0; k < maLibFilm.size(); k++) {

						if (listFilm.get(j2).getNom()
								.equals(maLibFilm.get(k).getNom())) {
							alreadyExist = true;
						}
					}
					if (!alreadyExist)
						maLibFilm.add(listFilm.get(j2));

				}
			}

		}
		newLibFilm.listFilm = maLibFilm;

		ArrayList<String> stringFilm2 = new ArrayList<String>();
		LibFilm newLibFilm2 = new LibFilm();
		stringFilm2 = newLibFilm.search(directeur, "Director");
		ArrayList<Film> maLibFilm2 = new ArrayList<Film>();
		for (int j = 0; j < stringFilm2.size(); j++) {
			for (int j2 = 0; j2 < newLibFilm.listFilm.size(); j2++) {

				String filmLowerCase = stringFilm2.get(j);
				if (newLibFilm.listFilm.get(j2).getNom().equals(filmLowerCase)) {

					boolean alreadyExist = false;
					for (int k = 0; k < maLibFilm2.size(); k++) {

						if (newLibFilm.listFilm.get(j2).getNom()
								.equals(maLibFilm2.get(k).getNom())) {
							alreadyExist = true;
						}
					}
					if (!alreadyExist)
						maLibFilm2.add(newLibFilm.listFilm.get(j2));
				}
			}

		}
		newLibFilm2.listFilm = maLibFilm2;

		ArrayList<String> stringFilm3 = new ArrayList<String>();
		LibFilm newLibFilm3 = new LibFilm();
		stringFilm3 = newLibFilm2.search(years, "Years");
		ArrayList<Film> maLibFilm3 = new ArrayList<Film>();
		for (int j = 0; j < stringFilm3.size(); j++) {
			for (int j2 = 0; j2 < newLibFilm2.listFilm.size(); j2++) {

				String filmLowerCase = stringFilm3.get(j);
				if (newLibFilm2.listFilm.get(j2).getNom().equals(filmLowerCase)) {
					boolean alreadyExist = false;
					for (int k = 0; k < maLibFilm3.size(); k++) {

						if (newLibFilm2.listFilm.get(j2).getNom()
								.equals(maLibFilm3.get(k).getNom())) {
							alreadyExist = true;
						}
					}
					if (!alreadyExist)
						maLibFilm3.add(newLibFilm2.listFilm.get(j2));
				}
			}

		}
		newLibFilm3.listFilm = maLibFilm3;

		ArrayList<String> stringFilm4 = new ArrayList<String>();
		stringFilm4 = newLibFilm3.search(genre, "Genre");
		return stringFilm4;

	}
	
	public ArrayList<Film> getListPlayingNow()
	{
		ArrayList<Film> returnList = new ArrayList<Film>();
		for (int i = 0; i < listFilm.size(); i++) 
		{
			if(listFilm.get(i).getDate()==2014)
			{
				returnList.add(listFilm.get(i));
			
			}
			if(returnList.size() >= 8)
			{
				break;
			}
		}
		return returnList;
	}
	
	public ArrayList<Film> getListWantToWatch()
	{
		return ConnectedActivity.mUtilisateur.getListFilmAVoir();
	}
	
	public ArrayList<Film> getListMostWatched()
	{
		ArrayList<Film> returnList = new ArrayList<Film>();
		returnList.add(this.getFilm(1));
		returnList.add(this.getFilm(6));
		returnList.add(this.getFilm(7));
		returnList.add(this.getFilm(14));
		returnList.add(this.getFilm(17));
		returnList.add(this.getFilm(23));
		returnList.add(this.getFilm(30));
		return returnList;
	}
	
	public ArrayList<Film> getListComingSoon()
	{
		ArrayList<Film> returnList = new ArrayList<Film>();
		returnList.add(this.getFilm(4));
		returnList.add(this.getFilm(9));
		returnList.add(this.getFilm(13));
		returnList.add(this.getFilm(33));
		returnList.add(this.getFilm(49));
		returnList.add(this.getFilm(53));
		return returnList;
	}
}